package com.ldk.admin.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldk.admin.common.ResponseResult;
import com.ldk.admin.constants.FORM;
import com.ldk.framework.entity.*;
import com.ldk.framework.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.admin.constants.ViewMode;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.enums.PlatformUserType;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 省公安厅
 * 1.用户注册信息
 * @author xiangwl
 * 2018年8月9日
 * com.ldk.admin.controller
 * SecyPassengerController
 */
@Controller
@RequestMapping("/secy")
public class SecyPassengerController extends BaseController {
    /**
     * 路由地址
     */
    private final String route = "secy";
    /**
     * 模块名称
     */
    private final String module = "用户注册信息";
    @Autowired
    private ISecyPassengerService secyPassengerService;

    @RequestMapping("/passenger/list")
    public ModelAndView list(ModelMap map,HttpServletRequest request,
                             HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);

        //setLinks(map, "", "", module+"管理", "/"+route+"/passenger/list", "添加"+module, "/"+route+"/passenger/add");
        setLinks(map, "", "", module+"管理", "/"+route+"/passenger/list", "", "");
        map.addAttribute("route", route);
        map.addAttribute("module", module);

        return new ModelAndView("/secy/passenger/list");
    }

    @ResponseBody
    @RequestMapping("/passenger/getList")
    public LigerUIGridResult<SecyPassenger> getList(@RequestParam(value="page",required=false,defaultValue="1") int page,
                                                @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
                                                @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
                                                HttpServletRequest request)
    {
        int limit = pageSize;
        int offset = (page -1) * pageSize;

        SecyPassenger query = new SecyPassenger();
        if(!StringUtils.isBlank(searchKey)){
            query.setSearchKey(searchKey);
        }
        CustomPageable pageable = new CustomPageable(offset, limit);

        Page<SecyPassenger> result = secyPassengerService.selectPageList("", query, pageable);

        LigerUIGridResult<SecyPassenger> grid = new LigerUIGridResult<>();
        grid.Total = result.getTotalElements();
        grid.Rows = result.getContent();

        return grid;
    }

    @RequestMapping("/passenger/add")
    public ModelAndView add(ModelMap map,HttpServletRequest request,
                            HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);
        setLinks(map, module+"管理", "/"+route+"/passenger/list", "添加"+module, "/"+route+"/passenger/add", "", "");
        setForm(map, "/"+route+"/passenger/insert", null);
        map.addAttribute("module", module);

        SecyPassenger model = new SecyPassenger();

        return new ModelAndView("/secy/passenger/info","model",model);
    }

    @RequestMapping("/passenger/edit/{id}")
    public ModelAndView edit(@PathVariable long id, ModelMap map,HttpServletRequest request,
                             HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);
        SecyPassenger query = new SecyPassenger();
        query.setId(id);

        SecyPassenger model = secyPassengerService.selectOne("", query);
        if(model == null){
            return new ModelAndView("redirect:/"+route+"/passenger/list");
        }
        setLinks(map, module+"管理", "/"+route+"/passenger/list", "编辑"+module, "/"+route+"/passenger/edit/"+id, "", "");
        setForm(map, "/"+route+"/passenger/update", null);
        map.addAttribute("module", module);

        return new ModelAndView("/secy/passenger/info","model", model);
    }

    @RequestMapping("/driverapp/insert")
    public ModelAndView insert(@ModelAttribute("model") SecyPassenger model,
                               RedirectAttributes redAttr, ModelMap map, HttpServletRequest request,
                               HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);
        ResponseResult rst = new ResponseResult();
        redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);
        try{

            int row = secyPassengerService.insert("",model);

            if(row > 0){
                rst.AddOk("添加"+module+"成功");
            }
            else{
                rst.AddError("添加"+module+"失败");
            }
        }
        catch(Exception e){
            rst.AddError("添加"+module+"失败");
            logger.error(e.getMessage());
        }
        return new ModelAndView("redirect:/"+route+"/passenger/add");
    }

    @RequestMapping("/passenger/update")
    public ModelAndView update(@ModelAttribute("model") SecyPassenger model,
                               RedirectAttributes redAttr, ModelMap map, HttpServletRequest request,
                               HttpServletResponse response)
    {
        //session是否过期
        this.isExpired(request,response);
        ResponseResult rst = new ResponseResult();

        redAttr.addAttribute("id", model.getId());
        redAttr.addFlashAttribute(FORM.FROM_RESULT, rst);

        try{
            int row = secyPassengerService.updateByKey("", model);
            if(row > 0){
                rst.AddOk("修改"+module+"成功");
            }
            else{
                rst.AddError("修改"+module+"失败");
            }
        }
        catch(Exception e){
            rst.AddError("修改"+module+"失败");
            logger.error(e.getMessage());
        }

        return new ModelAndView("redirect:/"+route+"/passenger/edit/{id}");
    }

    /**
     * 浏览
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/passenger/view/{id}")
    public ModelAndView view(@PathVariable long id, ModelMap map,HttpServletRequest request,
                             HttpServletResponse response){
        //session是否过期
        this.isExpired(request,response);

        setViewMode(map, ViewMode.View);
        SecyPassenger query = new SecyPassenger();
        query.setId(id);
        SecyPassenger model = secyPassengerService.selectOne("", query);

        map.addAttribute("module", module);

        return new ModelAndView("/secy/passenger/info","model", model);
    }

    @ResponseBody
    @RequestMapping("/passenger/remove/{id}")
    public ResponseResult remove(@PathVariable long id, HttpServletRequest request){

        ResponseResult rst = new ResponseResult();
        if(id < 1)
        {
            rst.AddError("删除"+module+"错误");
            return rst;
        }
        Boolean successed = false;
        try{
            SecyPassenger query = new SecyPassenger();
            query.setId(id);
            int row = secyPassengerService.deleteByKey("", query);
            successed = row > 0;
        }
        catch(Exception e){
            logger.error(e.getMessage());
        }
        if(successed){
            rst.AddOk("删除"+module+"成功");
        }
        else{
            rst.AddError("删除"+module+"失败");
        }

        return rst;
    }
}
